package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.JsonUtil;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import com.fywsy.fyweb.utils.TextUtils;
import com.google.gson.Gson;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 长安幻想
 *
 *
 * 110.42.43.131
 * http://110.42.43.131:3030/mail/add
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(3)
public class AChanganhuanxiangStrategy extends BaseGameStrategy {

    public static final String URL = "http://114.66.45.141:30382/gmss/user/gmquery.php";

    //自己写的接口↓
    public static final String QUERY_ID = "http://114.66.45.141:30382/gmss/user/get_id.php";

    public static final String REAL_MAIL = "http://114.66.45.141:30382/gmss/user/send_mail.php";


    /**
     * type: mail
     * uid: 100124
     * param: 道具:白虎牙:3
     * qu: 1
     * checknum: 123456
     *
     *
     * 物品
     * @param item
     * @return
     */
    @GongnengMethod(31)
    public String daoju(SendItem item) {
        return sendMail("道具",item);
    }

    /**
     * 大量道具
     * @param item
     * @return
     */
    @GongnengMethod(39)
    public String largeDaoju(SendItem item) {
        return sendMail("道具",item);
    }


    /**
     * 经验
     * @param item
     * @return
     */
    @GongnengMethod(32)
    public String exp(SendItem item) {
        return sendMail("经验",item);
    }

    /**
     * 坐骑
     * @param item
     * @return
     */
    @GongnengMethod(33)
    public String zuoqi(SendItem item) {
        return sendMail("坐骑",item);
    }

    /**
     * 坐骑
     * @param item
     * @return
     */
    @GongnengMethod(34)
    public String shizhuang(SendItem item) {
        return sendMail("时装",item);
    }


    /**
     * 宠物
     * @param item
     * @return
     */
    @GongnengMethod(35)
    public String chongwu(SendItem item) {
        return sendMail("宠物",item);
    }

    /**
     * 红色法宝
     * @param item
     * @return
     */
    @GongnengMethod(36)
    public String fabao(SendItem item) {
        return sendMail("法宝",item);
    }

    /**
     * 坐骑魂玉
     * @param item
     * @return
     */
    @GongnengMethod(37)
    public String zuoqihunyu(SendItem item) {
        return sendMail("坐骑魂玉",item);
    }

    @GongnengMethod(38)
    public String huobi(SendItem item) {
        return sendMail("资产",item);
    }

    /**
     * 定制宠物
     * @param item
     * @return
     */
    @GongnengMethod(40)
    public String dzChongwu(SendItem item) {
        String id = getId(item.uid);
        if(TextUtils.isEmpty(id)) return "发送失败,角色不存在";

        //[{"type":"宠物_定制","name":"神兽小白龙","template":"宠物#5爆20技","is_dz":0,"num":1}]
        Map<String,String> cwu = new HashMap();
        cwu.put("type", "宠物_定制");
        cwu.put("name", item.itemId);
        cwu.put("template","宠物#5爆20技");
        cwu.put("is_dz","0");
        cwu.put("num","1");

        List<Map<String,String>> fromat = new ArrayList<>();
        fromat.add(cwu);

        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("rids",id);
        params.add("format",new Gson().toJson(fromat));

        return RestTemplateUtil.sendPostForm(REAL_MAIL, params);
    }




    private String sendMail(String tag,SendItem item) {
        String id = getId(item.uid);
        if(TextUtils.isEmpty(id)) return "发送失败,角色不存在";

        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("type","mail");
        params.add("uid",id);
        params.add("qu","1");
        params.add("param",tag + ":" + item.itemId + ":" + item.number);
        params.add("checknum","123456");

        return RestTemplateUtil.sendPostForm(URL, params);
    }

    /**
     * 通过名字查询id
     * type=chaxun&account=18655556666&qu=1&checknum=123456
     * @param name
     */
    private String getId(String name) {
        if(TextUtils.isEmpty(name)) return null;
        Map<String,String> params = new HashMap<>();
        params.put("name",name);
        return RestTemplateUtil.sendPostJsonAndGetJsonKey(QUERY_ID, params, "id");
    }


    public static void main(String[] args) {
        SendItem item = new SendItem();
        item.number = "2";
        item.uid = "风云";
        item.itemId = "天宝图";
        String s = new AChanganhuanxiangStrategy().daoju(item);
        System.out.println(s);
    }
}

